<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格" prop="productSpecification">
        <el-input
          v-model="queryParams.productSpecification"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="getList"
        >刷新</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-view"
          size="mini"
          @click="getAllData"
        >查看全部</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-data-line"
          size="mini"
          @click="getTestData"
        >测试数据</el-button>
      </el-col>
      <el-col :span="12">
        <span class="data-info">
          共找到 <strong>{{ hotActivityList.length }}</strong> 条热门商品数据
        </span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotActivityList" stripe>
      <el-table-column label="商品名称" align="center" prop="product_name" width="200" show-overflow-tooltip />
      <el-table-column label="商品规格" align="center" prop="product_specification" width="250" show-overflow-tooltip />
      <el-table-column label="参团总人数" align="center" prop="total_participants" width="120">
        <template slot-scope="scope">
          <el-tag type="success" size="small">{{ scope.row && scope.row.total_participants ? scope.row.total_participants : 0 }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="相关拼团数" align="center" prop="related_group_count" width="120">
        <template slot-scope="scope">
          <el-tag type="info" size="small">{{ scope.row && scope.row.related_group_count ? scope.row.related_group_count : 0 }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <div v-if="hotActivityList.length === 0 && !loading" class="empty-data">
      <el-empty description="暂无热门商品数据"></el-empty>
    </div>
  </div>
</template>

<script>
import { getHotGroupActivities, getAllGroupActivities, getTestData } from "@/api/group/user_active"

export default {
  name: "HotGroupActivities",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 热门活动表格数据
      hotActivityList: [],
      // 查询参数
      queryParams: {
        productName: null,
        productSpecification: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询热门商品列表 */
    getList() {
      this.loading = true
      console.log('=== 前端：开始请求热门商品数据 ===')
      getHotGroupActivities().then(response => {
        console.log('=== 前端：收到响应 ===', response)
        let allData = response.data || []
        console.log('=== 前端：原始数据 ===', allData)
        // 确保数据是数组且过滤掉null/undefined项
        if (Array.isArray(allData)) {
          allData = allData.filter(item => item !== null && item !== undefined)
        } else {
          allData = []
        }
        console.log('=== 前端：过滤后数据 ===', allData)
        // 根据查询参数过滤数据
        this.hotActivityList = this.filterData(allData)
        console.log('=== 前端：最终显示数据 ===', this.hotActivityList)
        this.loading = false
      }).catch(error => {
        console.error('=== 前端：获取热门商品数据失败 ===', error)
        this.hotActivityList = []
        this.loading = false
      })
    },
    /** 根据查询参数过滤数据 */
    filterData(data) {
      return data.filter(item => {
        if (!item) return false
        
        const matchProductName = !this.queryParams.productName || 
          (item.product_name && item.product_name.toLowerCase().includes(this.queryParams.productName.toLowerCase()))
        const matchProductSpecification = !this.queryParams.productSpecification || 
          (item.product_specification && item.product_specification.toLowerCase().includes(this.queryParams.productSpecification.toLowerCase()))
        
        return matchProductName && matchProductSpecification
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 查看全部数据 */
    getAllData() {
      this.loading = true
      getAllGroupActivities().then(response => {
        let allData = response.data || []
        this.hotActivityList = this.filterData(allData)
        this.loading = false
        this.$message.success(`加载了 ${allData.length} 条全部数据`)
      }).catch(() => {
        this.loading = false
      })
    },
    /** 获取测试数据 */
    getTestData() {
      this.loading = true
      getTestData().then(response => {
        this.hotActivityList = response.data || []
        this.loading = false
        this.$message.success(`加载了 ${this.hotActivityList.length} 条测试数据`)
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
.data-info {
  color: #606266;
  font-size: 14px;
  line-height: 32px;
}

.data-info strong {
  color: #409EFF;
  font-weight: bold;
}

.empty-data {
  text-align: center;
  padding: 40px 0;
}
</style>
